﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cn.Edwon.Web.Support;
using Cn.Edwon.Spring;
using Cn.Cellula.Biz.Port;
using System.Reflection;
using Cn.Cellula.Client;
using Cn.Cellula.Biz;
using Cn.Cellula.Model.System.Wf;
using Cn.Edwon.Web.Validation;
using Cn.Edwon.Web;

namespace Cn.Cellula.Website.Admin.Wf.Config {
    public partial class EditNode : PageSupport {
        IWfBiz biz = BizFactory.Get<IWfBiz>();
        String EOKey = null;
        protected void Page_Load(object sender, EventArgs e) {
            EOKey = Request.QueryString["id"];
            if (!Page.IsPostBack) {
                InitForm();
                if (!String.IsNullOrEmpty(EOKey)) {
                    FillForm();
                }
            }
        }

        private void InitForm() {
            if (!String.IsNullOrEmpty(Request.QueryString["wid"])) {
                this.ddl_wf.DataSource = biz.LoadAll<TSysWfEO>();
                this.ddl_wf.DataBind();
            }
        }

        /// <summary>
        /// 表单初始化数据
        /// </summary>
        /// <param name="typeKey"></param>
        private void FillForm() {
            try {
                VSysWfNodeEO eo = biz.Load<VSysWfNodeEO>(EOKey);
                this.txt_NodeName.Text = eo.NodeName;
                this.txt_Role.Text = eo.RoleName;
                this.txt_RuleDesc.Text = eo.RuleDesc;
                this.txt_User.Text = eo.UserName;
                this.ddl_wf.SelectedValue = eo.WfKey;
                this.hid_Role.Value = eo.RoleKey;
                this.hid_User.Value = eo.UserKey;
            } catch (Exception ex) {
                log.Error(ex.Message);
                this.txt_NodeName.Text = "加载数据异常";
                this.btn_save.Enabled = false;
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_save_Click(object sender, EventArgs e) {
            Boolean bk = QuickValidator.Execute(Request, this.txt_NodeName.Parent);
            if (!bk) {
                log.Error(QuickValidator.Message);
                SiteInfo.Send(Page, "", QuickValidator.Message);
                return;
            }
            IUserBiz ubiz = BizFactory.Get<IUserBiz>();
            //---- 增加新项 ---------------------------------
            if (String.IsNullOrEmpty(EOKey)) {
                TSysWfNodeEO eo = new TSysWfNodeEO();
                eo.CreateDt = DateTime.Now;
                eo.NodeName = this.txt_NodeName.Text;
                eo.LastDt = DateTime.Now;
                eo.Operator = this.Actor.ID;
                eo.RoleKey = this.hid_Role.Value;
                eo.UserKey = this.hid_User.Value;
                eo.WfKey = this.ddl_wf.SelectedValue;
                String newKey = biz.Save<TSysWfNodeEO>(eo).ToString();
                BizFactory.Get<ILogBiz>().LogInsert("流程节点", eo.NodeName, this.Actor, Request);
                Response.Redirect("List.aspx?wid=" + eo.WfKey + "&nid=" + newKey);
            } else {//更新----------------------------------
                TSysWfNodeEO eo = biz.Load<TSysWfNodeEO>(EOKey);
                eo.NodeName = this.txt_NodeName.Text;
                eo.LastDt = DateTime.Now;
                eo.Operator = this.Actor.ID;
                eo.RoleKey = this.hid_Role.Value;
                eo.UserKey = this.hid_User.Value;
                eo.WfKey = this.ddl_wf.SelectedValue;
                BizFactory.Get<ILogBiz>().LogUpdate("流程节点", eo.NodeName, this.Actor, Request);
                Response.Redirect("List.aspx?wid=" + eo.WfKey + "&nid=" + eo.NodeKey);
            }
        }
    }
}